<template>
  <div class="home-page">
    <div class="search-and-filter">
      <input type="text" v-model="searchParams.name" placeholder="按名称搜索...">
      <input type="text" v-model="searchParams.region" placeholder="按地区搜索...">
      <input type="text" v-model="searchParams.tag" placeholder="按标签搜索...">
      <button @click="search">搜索</button>
      <select v-model="sortOrder" @change="sortSpots">
        <option value="rating_desc">按评分高到低</option>
        <option value="rating_asc">按评分低到高</option>
      </select>
    </div>
    <div class="spot-list">
      <div v-for="spot in spots" :key="spot.id" class="spot-card" @click="goToSpot(spot.id)">
        <img :src="spot.image" :alt="spot.name">
        <div class="spot-info">
          <h3>{{ spot.name }}</h3>
          <p>{{ spot.description }}</p>
          <div class="spot-meta">
            <span>{{ spot.address }}</span>
            <span>评分: {{ spot.rating }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex';

export default {
  name: 'HomePage',
  data() {
    return {
      searchParams: {
        name: '',
        region: '',
        tag: ''
      },
      sortOrder: 'rating_desc'
    };
  },
  computed: {
    ...mapGetters('spot', { spots: 'allSpots' })
  },
  methods: {
    ...mapActions('spot', ['fetchSpots']),
    search() {
      this.fetchSpots(this.searchParams);
    },
    sortSpots() {
      if (this.sortOrder === 'rating_desc') {
        this.spots.sort((a, b) => b.rating - a.rating);
      } else {
        this.spots.sort((a, b) => a.rating - b.rating);
      }
    },
    goToSpot(id) {
      this.$router.push(`/spot/${id}`);
    }
  },
  created() {
    this.fetchSpots().then(() => {
        this.sortSpots();
    });
  }
}
</script>

<style scoped>
.home-page {
  max-width: 1000px;
  margin: 0 auto;
  padding: 30px 10px;
}
.search-and-filter {
  margin-bottom: 20px;
  padding: 15px;
  background: #f6fffa;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(66, 185, 131, 0.08);
  display: flex;
  align-items: center;
  gap: 10px;
}
.search-and-filter input, .search-and-filter select, .search-and-filter button {
  border: 1px solid #b2e5d6;
  background: #f6fffa;
  color: #2c3e50;
}
.search-and-filter button {
  background: #42b983;
  color: #fff;
}
.spot-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}
.spot-card {
  background: #f6fffa;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(66, 185, 131, 0.08);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s;
  border: 1px solid #b2e5d6;
}
.spot-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(66, 185, 131, 0.15);
}
.spot-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}
.spot-info {
  padding: 15px;
}
.spot-info h3 {
  margin-top: 0;
  color: #2ca06c;
}
.spot-meta {
  display: flex;
  justify-content: space-between;
  color: #42b983;
  font-size: 14px;
  margin-top: 10px;
}
</style> 